package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class _178NewsAnalyse implements AnalyseNews{

	private static final String[] regex={
				"http://.*.178.com/[0-9]{6}/[0-9]*.html"};
			
			public boolean isDetailPage(String url) {
				for (int i=0; i < regex.length; i++) {
					if (url.matches(regex[i])) {
						return true;
					}
				}
				return false;
			}

			
			public NewsMeta Update(NewsMeta meta) {
				return null;
			}



			
			public NewsMeta parserHtml(UrlMeta urlMeta) {
				NewsMeta news=new NewsMeta();
				if (urlMeta.getHtml() == null) {
				}
				String htmltxt=urlMeta.getHtml();
				String url=urlMeta.getUrl();
				if(!isDetailPage(url)){
				}


				String title=null;
				String content=null;
				String author=null;
				Long date=null;
				Document doc=Jsoup.parse(htmltxt);
				doc.select(".gameinfo-box,.video-box,.relevant,.sidebar").remove();
				title=doc.select(".nrzw_bts").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("div.title h2").text();
					if(StringUtils.isBlank(title)){
						title=doc.select("div.title h1").text();
						if(StringUtils.isBlank(title)){
							title=doc.select("h2.base_title").text();
							if(StringUtils.isBlank(title)){
								title=doc.select("div#contentTitle h1").text();
								if(StringUtils.isBlank(title)){
									title=doc.select("div.arc-text h1").text();
									if(StringUtils.isBlank(title)){
										title=doc.select("h1.title").text();
									}
								}
							}
							
						}
					}
				}
				if(StringUtils.isBlank(title)){
					title=doc.select("div#content-header h2").text();
				}
				content=HtmlCleaner.getContentHtml(url, doc.select("div#text"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url, doc.select("div.text"));
				}
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url, doc.select(".nrzw_txts"));
				}
				if(StringUtils.isBlank(content)){
					content =HtmlCleaner.getContentHtml(url,  doc.select(".tx p"));
				}
				if(StringUtils.isBlank(content)){
					content =HtmlCleaner.getContentHtml(url,  doc.select("div#main-content"));
				}
				if(StringUtils.isBlank(content)){
					content =HtmlCleaner.getContentHtml(url,  doc.select("div#contentText"));
				}
				if(StringUtils.isBlank(content)){
					content =HtmlCleaner.getContentHtml(url,  doc.select("div.txt"));
				}
				author=JSoupUtils.matchAuthor(doc, "来源:");

				date=JSoupUtils.matchDate(doc,"发布时间","来源：");
				if(date == null){
					date=DateUtils.matchDate(doc.select("div.title").text());
					if(date == null){
						date=DateUtils.matchDate(doc.select("div.title-info").text());
						if(date == null){
							date=DateUtils.matchDate(doc.select("div#source_text").text());
							if(date == null){
								date=DateUtils.matchDate(doc.select("div.top").text());
								if(date == null){
									date=DateUtils.matchDate(doc.select("div.text").text());
									if(date == null){
										date=DateUtils.matchDate(doc.select("div.title_data").text());
										if(date == null){
											date=DateUtils.matchDate(doc.select("div.other").text());
											if(date == null){
												date=DateUtils.matchDate(doc.select("p#content-meta").text());
												
											}
											if(date == null){
												date=DateUtils.matchDate(doc.select("span#publicDate").text());
												if(date == null){
													date=DateUtils.matchDate(doc.select("span").text());
													
												}
											}
										}
									}
								}
							}
						}
					}

				}
				if(StringUtils.isBlank(title)){
				}
				news.setUrl(url);
				news.setTitle(title);
				news.setContent(content);
				news.setAuthor(author);
				news.setDate(date);
				return news;
			}

			public static void main(String[] args) {
				_178NewsAnalyse test=new _178NewsAnalyse();

				String url="http://xin.178.com/201512/243212299845.html";
				System.out.println(test.isDetailPage(url));
				UrlMeta meta=CrawlHTML.responseToURL(url);
				NewsMeta parserHtml=test.parserHtml(meta);
				System.out.println(parserHtml);
			}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
